package com.foreknow.servlet;

import com.foreknow.entity.User;
import com.foreknow.service.UserService;
import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

@WebServlet({"/api/sign-in","/api/sign-up"})
public class UserServlet extends HttpServlet {
    private UserService userService;
    public UserServlet() {
        super();
        userService = new UserService();
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String uri = req.getRequestURI();
        //要将登录成功或失败的一些提示信息保存到一个Map集合
        Map<String,Object> data = new HashMap<>();
        Gson gson = new Gson();
        if(uri.endsWith("/api/sign-in")){
            //获取请求的参数
            String account = req.getParameter("account");
            String password = req.getParameter("password");
            User user = userService.Login(account,password);
            if(user!=null){
                data.put("errorCode",0);
                data.put("message","登录成功!");
                //根据姓名查询到当前的用户信息并保存到map中
                //将密码设置为空(为了安全)
                user.setPassword("");
                data.put("user",user);

            }else {
                data.put("errorCode",-1);
                data.put("message","登录失败!");
                data.put("user",null);
            }
        }else if(uri.endsWith("/api/sign-up")){

            String username = req.getParameter("username");
            String account = req.getParameter("account");
            String password = req.getParameter("password");
            String phone = req.getParameter("phone");
            String email = req.getParameter("email");
            String avater = req.getParameter("avater");

            User user = new User();
            user.setUsername(username);
            user.setAccount(account);
            user.setPassword(password);
            user.setPhone(phone);
            user.setEmail(email);
            user.setAvater(avater);

            if(userService.Register(user)){
                user = userService.getUserByName(username);
                data.put("errorCode",0);
                data.put("message","注册成功，正在登录!");
                user.setPassword("");
                data.put("user",user);
            }else {
                data.put("errorCode",-1);
                data.put("message","注册失败!");
            }
        }
        //将map对象转成json字符串
        String json = gson.toJson(data);
        resp.getWriter().write(json);
        data.clear();
    }
}
